package com.banciyuan.bcywebview.biz.detail.complex.holder;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.text.SpannableString;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.base.factory.TimelineFactory;
import com.banciyuan.bcywebview.base.helper.data.UserDataHelper;
import com.banciyuan.bcywebview.base.helper.timelinedatacenter.TimelineLikeHelper;
import com.banciyuan.bcywebview.base.util.BaseObserver;
import com.banciyuan.bcywebview.base.view.tag.TagView;
import com.banciyuan.bcywebview.base.view.textview.ExpandedTextView;
import com.banciyuan.bcywebview.base.view.toast.MyToast;
import com.banciyuan.bcywebview.biz.circles.smooth.CircleSmoothActivity;
import com.banciyuan.bcywebview.biz.circles.smooth.CircleTagSmoothActivity;
import com.banciyuan.bcywebview.biz.event.EventDetailActivity;
import com.banciyuan.bcywebview.biz.login.LoginActivity;
import com.banciyuan.bcywebview.utils.constants.MyConstants;
import com.banciyuan.bcywebview.utils.http.StatusCodeUtils;
import com.banciyuan.bcywebview.utils.intent.gotoUtil;
import com.banciyuan.bcywebview.utils.string.StringUtil;
import com.banciyuan.bcywebview.utils.universalimageloader.core.ImageLoader;
import com.banciyuan.bcywebview.utils.universalimageloader.utils.ImageOptions;

import de.greenrobot.daoexample.model.Complex;
import de.greenrobot.daoexample.model.TagDetail;

/**
 * Created by squall on 2017/6/7.
 */

public class VideoHeaderHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    private TextView mVideoTitle;
    private TextView mVideoCount;
    private TextView mVideoTime;
    private TextView mUsername;
    private TextView mUserIntro;
    private ExpandedTextView mVideoIntro;
    private ImageView mUserheader;
    private TextView mUserFoucs;
    private View mVideInfo;

    private TagView tags;

    private LinearLayout lockcontral_ll;
    private LinearLayout needFans_ll;
    private TextView needFans_tv;
    private RelativeLayout lock_rl;
    private TextView lockReason_tv;

    private LinearLayout login_ll;
    private TextView login_tv;

    private Context mContext;
    private Complex complex;


    public VideoHeaderHolder(View itemView) {
        super(itemView);
        mVideoTitle = (TextView) itemView.findViewById(R.id.video_title);
        mVideoCount = (TextView) itemView.findViewById(R.id.video_viewcount);
        mVideoTime = (TextView) itemView.findViewById(R.id.video_time);
        mVideoIntro = (ExpandedTextView) itemView.findViewById(R.id.tv_video_intro);
        mUsername = (TextView) itemView.findViewById(R.id.user_name);
        mUserFoucs = (TextView) itemView.findViewById(R.id.user_focus);
        mUserIntro = (TextView) itemView.findViewById(R.id.user_intro);
        mUserheader = (ImageView) itemView.findViewById(R.id.detail_header);
        mVideInfo = itemView.findViewById(R.id.info_container);
        tags = (TagView) itemView.findViewById(R.id.float_tags);
        needFans_ll = (LinearLayout) itemView.findViewById(R.id.ll_needfans);
        needFans_tv = (TextView) itemView.findViewById(R.id.tv_setfans);
        lock_rl = (RelativeLayout) itemView.findViewById(R.id.ll_detail_locked);
        lockcontral_ll = (LinearLayout) itemView.findViewById(R.id.fix_container);

        lockReason_tv = (TextView) itemView.findViewById(R.id.locked_reason);
        login_tv = (TextView) itemView.findViewById(R.id.tv_login);
        login_ll = (LinearLayout) itemView.findViewById(R.id.ll_needlogin);
    }

    public void setData(Complex mItem, final Context mContext) {

        this.mContext = mContext;
        this.complex = mItem;
        if ((mItem.getStatus() == 1 || mItem.getStatus() == MyConstants.NEED_FANS) && mItem.getLock() == null) {
            if (mItem.getStatus() == 1) {
                if (mItem.getUid().equals(UserDataHelper.getInstance(mContext).getUid())) {
                    mUserFoucs.setVisibility(View.INVISIBLE);
                } else {
                    if (mItem.getProfile().getFollowstate().equals("unfollow")) {
                        mUserFoucs.setVisibility(View.VISIBLE);
                    } else {
                        mUserFoucs.setVisibility(View.GONE);
                    }
                }
                mVideoTime.setText(StringUtil.getOppositeTime(mItem.getCtime()));
                if (!TextUtils.isEmpty(complex.getTitle())) {
                    mVideoTitle.setText(Html.fromHtml(complex.getTitle()));
                }
                if (!TextUtils.isEmpty(complex.getSummary())) {
                    mVideoIntro.setVisibility(View.VISIBLE);
                    mVideoIntro.setText(new SpannableString(StringUtil.htmlDecoder(complex.getSummary())), true, new ExpandedTextView.OnProcessClick() {
                        @Override
                        public SpannableString onProcessSucc(CharSequence text) {
                            return new SpannableString(text);
                        }
                    }, new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            mVideoIntro.setMaxLines(200);
                        }
                    }, R.string.open_more_no_format, 2);
                } else {
                    mVideoIntro.setVisibility(View.GONE);
                }
                mVideoCount.setText(String.format(mContext.getString(R.string.view_count), complex.getView_count()));
                if (complex.getRela_circle() != null && !complex.getRela_circle().isEmpty()) {
                    ((ViewGroup) (tags.getParent())).setVisibility(View.VISIBLE);
                    tags.setTlist(complex.getRela_circle());
                    tags.setTagViewClick(new TagView.OnTagClickListener() {
                        @Override
                        public void onClick(TagDetail t) {
                            if (!TextUtils.isEmpty(t.getWork())) {
                                if (!TextUtils.isEmpty(t.getWid())) {
                                    Intent intent = new Intent(mContext, CircleSmoothActivity.class);
                                    intent.putExtra(gotoUtil.INTENT_VALUE_ONE, t.getWork());
                                    intent.putExtra(gotoUtil.INTENT_VALUE_TWO, t.getWid());
                                    mContext.startActivity(intent);
                                }
                            } else if (!TextUtils.isEmpty(t.getEvent_id())) {
                                gotoUtil.gotoAct(mContext, EventDetailActivity.class, t.getEvent_id());
                            } else {
                                Intent intent = new Intent(mContext, CircleTagSmoothActivity.class);
                                intent.putExtra(gotoUtil.INTENT_VALUE_ONE, t.getTag_name());
                                intent.putExtra("type", "tag");
                                mContext.startActivity(intent);
                            }
                        }
                    });
                } else {
                    ((ViewGroup) (tags.getParent())).setVisibility(View.GONE);
                }
            }
            if (mItem.getStatus() == MyConstants.NEED_FANS) {
                needFans_ll.setVisibility(View.VISIBLE);
                mVideInfo.setVisibility(View.GONE);
            }
            ImageLoader.getInstance().displayImage(mItem.getProfile().getAvatar(), mUserheader, ImageOptions.optionHead);
            mUsername.setText(mItem.getProfile().getUname());
            if (TextUtils.isEmpty(mItem.getProfile().getSelf_intro())) {
                mUserIntro.setVisibility(View.GONE);
            } else {
                mUserIntro.setVisibility(View.VISIBLE);
                mUserIntro.setText(Html.fromHtml(mItem.getProfile().getSelf_intro()));
            }

        } else if (mItem.getStatus() == MyConstants.NEED_LOGIN) {
            mVideInfo.setVisibility(View.GONE);
            login_ll.setVisibility(View.VISIBLE);
            login_tv.setVisibility(View.VISIBLE);
        } else if (mItem.getStatus() == MyConstants.WORK_LOKED) {
            mVideInfo.setVisibility(View.GONE);
            lock_rl.setVisibility(View.VISIBLE);
            lockcontral_ll.setVisibility(View.GONE);
            setLock();
        } else if (mItem.getLock() != null) {
            mVideInfo.setVisibility(View.GONE);
            BaseObserver.getInstance().notifyWatchers(BaseObserver.DELETE);
            lock_rl.setVisibility(View.VISIBLE);
            lockcontral_ll.setVisibility(View.GONE);
            if (!TextUtils.isEmpty(mItem.getLock().getReason())) {
                lockReason_tv.setText(Html.fromHtml(mItem.getLock().getReason()));
            } else {
                setLock();
            }
        }
        mUserheader.setOnClickListener(this);
        mUsername.setOnClickListener(this);
        mUserIntro.setOnClickListener(this);
        login_tv.setOnClickListener(this);
        mUserFoucs.setOnClickListener(this);
        needFans_tv.setOnClickListener(this);
    }

    private void setLock() {
        switch (complex.getTimelineType()) {
            case TimelineFactory.TIMELINE_TYPE_COSER:
                lockReason_tv.setText(mContext.getString(R.string.cos_locked_one));
                break;
            case TimelineFactory.TIMELINE_TYPE_ILLUST:
                lockReason_tv.setText(mContext.getString(R.string.draw_locked_one));
                break;
            case TimelineFactory.TIMELINE_TYPE_WRITER:
                lockReason_tv.setText(mContext.getString(R.string.write_locked_one));
                break;
            case TimelineFactory.TIMELINE_TYPE_DAILY:
                lockReason_tv.setText(mContext.getString(R.string.daily_locked_one));
                break;
        }
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.detail_header:
            case R.id.user_name:
            case R.id.user_intro:
                BaseObserver.getInstance().notifyWatchers(BaseObserver.GO_PERSON);
                break;
            case R.id.user_focus:
                TimelineLikeHelper.doFollow(mContext, "dofollow", complex.getProfile().getUid(), new TimelineLikeHelper.FollowSuccess() {
                    @Override
                    public void onFollowSuccess(String response) {
                        if (StatusCodeUtils.dealStatusCode(response, mContext)) {
                            mUserFoucs.setVisibility(View.GONE);
                            MyToast.show(mContext, mContext.getString(R.string.focus_succ));
                        }
                    }
                });
                break;
            case R.id.tv_setfans:
                TimelineLikeHelper.doFollow(mContext, "dofollow", complex.getProfile().getUid(), new TimelineLikeHelper.FollowSuccess() {
                    @Override
                    public void onFollowSuccess(String response) {
                        if (StatusCodeUtils.dealStatusCode(response, mContext)) {
                            MyToast.show(mContext, mContext.getString(R.string.focus_succ));
                            BaseObserver.getInstance().notifyWatchers(BaseObserver.FOLLOW_EVENT_REFREASH);
                        }
                    }
                });
                break;
            case R.id.tv_login:
                gotoUtil.gotoAct(mContext, LoginActivity.class);
                break;
        }
    }
}
